//package work.flow.demo.config;
//
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.statemachine.config.EnableStateMachine;
//import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
//import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
//import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
//import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
//import org.springframework.statemachine.listener.StateMachineListener;
//import org.springframework.statemachine.listener.StateMachineListenerAdapter;
//import org.springframework.statemachine.state.State;
//import work.flow.demo.entity.test01.LightEvent;
//import work.flow.demo.entity.test01.LightState;
//import work.flow.demo.entity.test02.LeaveEvents;
//import work.flow.demo.entity.test02.LeaveStates;
//
//import java.util.EnumSet;
//
//@Configuration
//@EnableStateMachine
//public class LeaveStateMachineConfig extends StateMachineConfigurerAdapter<LeaveStates, LeaveEvents> {
//
//    @Override
//    public void configure(StateMachineStateConfigurer<LeaveStates, LeaveEvents> states) throws Exception {
//        states.withStates()//
//                .initial(LeaveStates.DRAFT)  //
//                .states(EnumSet.allOf(LeaveStates.class));//
//    }
//
//    @Override
//    public void configure(StateMachineTransitionConfigurer<LeaveStates, LeaveEvents> transitions) throws Exception {
//        transitions
//                // 从草稿提交申请
//                .withExternal()
//                .source(LeaveStates.DRAFT)
//                .target(LeaveStates.SUBMITTED)
//                .event(LeaveEvents.SUBMIT)
//                .and()
//                // 部门主管批准
//                .withExternal()
//                .source(LeaveStates.SUBMITTED)
//                .target(LeaveStates.DEPT_APPROVED)
//                .event(LeaveEvents.DEPT_APPROVE)
//                .and()
//                // 部门主管驳回
//                .withExternal()
//                .source(LeaveStates.SUBMITTED)
//                .target(LeaveStates.REJECTED)
//                .event(LeaveEvents.DEPT_REJECT)
//                .and()
//                // HR批准
//                .withExternal()
//                .source(LeaveStates.DEPT_APPROVED)
//                .target(LeaveStates.HR_APPROVED)
//                .event(LeaveEvents.HR_APPROVE)
//                .and()
//                // HR驳回
//                .withExternal()
//                .source(LeaveStates.DEPT_APPROVED)
//                .target(LeaveStates.REJECTED)
//                .event(LeaveEvents.HR_REJECT)
//                .and()
//                // 从驳回状态修改申请
//                .withExternal()
//                .source(LeaveStates.REJECTED)
//                .target(LeaveStates.DRAFT)
//                .event(LeaveEvents.MODIFY)
//                .and()
//                // HR最终确认完成
//                .withExternal()
//                .source(LeaveStates.HR_APPROVED)
//                .target(LeaveStates.COMPLETED)
//                .event(LeaveEvents.HR_APPROVE);
//    }
//
//    @Override
//    public void configure(StateMachineConfigurationConfigurer<LeaveStates, LeaveEvents> config) throws Exception {
//        config.withConfiguration().autoStartup(true)  // 确保自动启动
//                .listener(listener());
//    }
//
//    @Bean
//    public StateMachineListener<LeaveStates, LeaveEvents> listener() {
//        return new StateMachineListenerAdapter<>() {
//            @Override
//            public void stateChanged(State<LeaveStates, LeaveEvents> from, State<LeaveStates, LeaveEvents> to) {
//                System.out.println("State changed from " + (from == null ? "null" : from.getId()) + " to " + to.getId());
//            }
//        };
//    }
//
//}
